//
//  SetMeViewController.m
//  WorkNest
//
//  Created by giantumbrella on 15/7/28.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "SetMeViewController.h"
#import "MobClick.h"

@interface SetMeViewController () <UIAlertViewDelegate>

@end

@implementation SetMeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleView:@"设置"];
//    self.view.backgroundColor = [UIUtil colorWithHexString:@"#ededf5"];
    
    [self createSubview];
}

#pragma mark 创建视图
- (void)createSubview {
    UIView *viewTopBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    [self.view addSubview:viewTopBg];
    viewTopBg.backgroundColor = [UIColor whiteColor];
    //图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 11, 17, 18)];
    imageView.image = [UIImage imageNamed:@"setInfo"];
    [viewTopBg addSubview:imageView];
    
    //文字
    UILabel *labelText = [[UILabel alloc] initWithFrame:CGRectMake(imageView.frame.size.width+imageView.frame.origin.x+5, 10, 100, 20)];
    [viewTopBg addSubview:labelText];
    labelText.text = @"消息提醒";
    labelText.textColor = [UIUtil colorWithHexString:@"#b2b3b4"];
    labelText.font = [UIFont systemFontOfSize:14];
    
    //开关
    UISwitch *kaiguan = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.8, 5, 60, 30)];
    [viewTopBg addSubview:kaiguan];
    
    //图片
    UIImageView *updateImageView = [[UIImageView alloc] initWithFrame:CGRectMake(90, 80, SCREEN_WIDTH-180, (SCREEN_WIDTH-180)*106/140)];
    updateImageView.image = [UIImage imageNamed:@"setUpdate"];
    [self.view addSubview:updateImageView];
    
    //检测新版本
    UIButton *checkVersonButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.2, SCREEN_HEIGHT*0.5, SCREEN_WIDTH*0.6, 35)];
    [self.view addSubview:checkVersonButton];
    [checkVersonButton setTitle:@"检测新版本" forState:UIControlStateNormal];
    [checkVersonButton setBackgroundImage:[UIImage imageNamed:@"addressSubmit"] forState:UIControlStateNormal];
    [checkVersonButton addTarget:self action:@selector(checkButtonVerson) forControlEvents:UIControlEventTouchUpInside];
}

- (void)checkButtonVerson {
    [ESHUDView showLoading];
    [MobClick checkUpdateWithDelegate:self selector:@selector(jumpToUpdate:)];
}

-(void)jumpToUpdate:(NSDictionary *)appInfo {
    [ESHUDView dismiss];
    BOOL update = [[appInfo objectForKey:@"update"] isEqualToString:@"YES"] ? YES : NO;
    if (update) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"有新版本是否更新"
                                                           delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"立即更新", nil];
        alertView.tag = 101011;
        [alertView show];
    }
    else {
        [[ESHUDView sharedInstance] showErrorMessage:@"暂未发现新版本" dismissBlock:nil];
    }
}
//进行更新
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 101011){
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",@"http://pre.im/gckj"]];
        [[UIApplication sharedApplication] openURL:url];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
